home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / hurd / fd-read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-30  |  2.6 KB  |  87 lines

  1. /* Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <hurd.h>
  22. #include <hurd/fd.h>
  23.  
  24. error_t
  25. _hurd_fd_read (struct hurd_fd *fd, void *buf, size_t *nbytes)
  26. {
  27.   error_t err;
  28.   char *data;
  29.   mach_msg_type_size_t nread;
  30.  
  31.   data = buf;
  32.   err = HURD_FD_PORT_USE
  33.     (fd,
  34.      ({
  35.        do
  36.      {
  37.        err = __io_read (ctty != MACH_PORT_NULL ? ctty : port,
  38.                 &data, &nread, -1, *nbytes);
  39.        if (ctty != MACH_PORT_NULL && err == EBACKGROUND)
  40.          {
  41.            /* We are a background job and tried to read from the tty.
  42.           We should probably get a SIGTTIN signal.  */
  43.            struct hurd_sigstate *ss;
  44.            if (_hurd_orphaned)
  45.          /* Our process group is orphaned.  Don't stop; just fail.  */
  46.          err = EIO;
  47.            else
  48.          {
  49.            ss = _hurd_self_sigstate ();
  50.            if (__sigismember (&ss->blocked, SIGTTIN) ||
  51.                ss->actions[SIGTTIN].sa_handler == SIG_IGN)
  52.              /* We are blocking or ignoring SIGTTIN.  Just fail.  */
  53.              err = EIO;
  54.            __mutex_unlock (&ss->lock);
  55.          }
  56.            if (err == EBACKGROUND)
  57.          {
  58.            /* Send a SIGTTIN signal to our process group.  */
  59.            err = __USEPORT (CTTYID, _hurd_sig_post (0, SIGTTIN, port));
  60.            /* XXX what to do if error here? */
  61.            /* At this point we should have just run the handler for
  62.               SIGTTIN or resumed after being stopped.  Now this is
  63.               still a "system call", so check to see if we should
  64.               restart it.  */
  65.            __mutex_lock (&ss->lock);
  66.            if (!(ss->actions[SIGTTIN].sa_flags & SA_RESTART))
  67.              err = EINTR;
  68.            __mutex_unlock (&ss->lock);
  69.          }
  70.          }
  71.      } while (err == EBACKGROUND);
  72.        err;
  73.      }));
  74.  
  75.   if (err)
  76.     return err;
  77.  
  78.   if (data != buf)
  79.     {
  80.       memcpy (buf, data, nread);
  81.       __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
  82.     }
  83.  
  84.   *nbytes = nread;
  85.   return 0;
  86. }
  87.